/**************************************************************************************************
 *  File Defination 
 *  - Ajax submitter
 -------------------------------------------------------------------  
 *  Run in PHP versions 4 and upper versions
 -------------------------------------------------------------------
 *  Apprain : Content Management Framework <http://www.apprain.com/>
 *  Download link: http://www.apprain.com/download
 *  Docs link: http://www.apprain.com/docs
 -------------------------------------------------------------------
 *  License text http://www.opensource.org/licenses/mit-license.php 
 *  About MIT license <http://en.wikipedia.org/wiki/MIT_License/>
*************************************************************************************************/

var apprain_a_s ={
	action				: base,
	form				: '#auto_chk_submit',
	debug				: false,
	res_flag			: '',
	res_msg				: '',
	auto_hide			: true,
	message_holder		: '.erros_message',
	loading_img			: base + '/images/loading.gif',
	show_debug			: function(response){
							if( apprain_a_s.debug)
							{
								alert( response );
							}
						},
	s_ready:			function ()
						{
							return (!apprain_f_v.error);
						},
	process_on_success: function()
						{
							$$(apprain_a_s.message_holder).set('html',apprain_a_s.res_msg);
							if( apprain_a_s.auto_hide )
							{
								$$(apprain_a_s.form).fade('out');
							}
						},
	process_on_error	: function()
						{
							$$(apprain_a_s.message_holder).set('html',apprain_a_s.res_msg);
						},
						loading_image: function(flag )
						{
							if( flag == 'show')
							{
								$$(apprain_a_s.message_holder).set('html','&nbsp;');
								$$(apprain_a_s.message_holder).setStyle("display","block");
								$$(apprain_a_s.message_holder).setStyle('background','url(' + apprain_a_s.loading_img + ') no-repeat');
							}
							else
							{
								$$(apprain_a_s.message_holder).setStyle('background','none');
							}
						},
						ajax_submit: function (){
							
							apprain_a_s.loading_image('show');
							$$(apprain_a_s.form).set('send', {
								method: 'post',
								onSuccess: function(responseTxt, responseXML) 
								{
									apprain_a_s.loading_image('hide');
									apprain_a_s.show_debug(responseTxt);

									responseObj = JSON.decode(responseTxt);
									apprain_a_s.res_flag = responseObj._status;
									apprain_a_s.res_msg = responseObj._message;
									
									if( apprain_a_s.res_flag == 'Success')
									{
										apprain_a_s.process_on_success();
									}
									else if( apprain_a_s.res_flag == 'Redirect')
									{
										
										window.location = responseObj._location;
									}
									else
									{
										apprain_a_s.process_on_error();
									}
								},
								onFailure: function(headerName, value) 
								{
								  alert('fail') 
								}
							});
							$$(apprain_a_s.form).send(apprain_a_s.action);
						},
	init				: function(e){		
							try
							{
								apprain_a_s.form = as_form_name;
								apprain_a_s.debug = as_debug;
								apprain_a_s.message_holder = as_message_element;
								apprain_a_s.auto_hide = as_auto_hide;
								apprain_a_s.loading_img = loading_img;
							}
							catch (err){}
							$$(apprain_a_s.message_holder).set('html','&nbsp;');
							apprain_a_s.loading_image('hide');
							apprain_a_s.action = $$(apprain_a_s.form).get('action');
						}
}